<?php
	if($session->read('Auth.User.group_id') == $session->read('Settings.adminPermission')){
		print '<ul>';
			print '<li ' . (($this->params['url']['url'] == 'admin/users') ? 'class="act"' : '') . '>' . $html->link(__('Users', true), array('controller' => 'users', 'action'=>'index', 'admin' => true)) . ' (' . $userCount . ')</li>';
			print '<li ' . (($this->params['url']['url'] == 'admin/communities') ? 'class="act"' : '') . '>' . $html->link(__('Communities', true), array('controller' => 'communities', 'action'=>'index', 'admin' => true)) . '</li>';
		print '</ul>';
		}
?>
<h2 class="account ful"><?php __('Profile');?></h2>
<div class="account">
<?php
echo $form->create('User', array('action' => 'edit', 'class' => 'nForm'));
	
//debug($user);
		print '<div class="mygravatar">';
			print $html->image('http://www.gravatar.com/avatar/'.md5(strtolower($session->read('Auth.User.email'))).'.jpg?s=60&d=' . urlencode('http://'.$_SERVER['SERVER_NAME'] . $html->url('/img/no_avatar.png')), array('title' => __('Your registered gravatar on http://gravatar.com', true), 'alt' => 'gravatar'));
		print '</div>';
		print '<div class="fl">';
			print $form->input('email', array('value' => $user['User']['email']));
			print $form->input('pass_change', array('value' => false, 'type' => 'password', 'label' => __('Password', true)));
			print $form->input('pass_confirm', array('type' => 'password', 'label' => __('Password confirm', true)));

			print '<dl>';
			
			print '<dt class="n">'.__('Group', true).'</dt>';
			print '<dd class="d">'.__($user['Group']['name'], true).'</dd>';

			if($user['User']['paid'] != '0000-00-00'){
				print '<dt class="n">'.__('Paid', true).'</dt>';
				print '<dd class="d">'.__($user['User']['paid'], true).'</dd>';
			}

			print '<dt class="n">'.__('Max accounts', true).'</dt>';
			print '<dd class="d">'.__($user['Group']['max_accounts'], true).'</dd>';

			print '<dt class="n">'.__('Registered', true).'</dt>';
			print '<dd class="d">'.__($user['User']['created'], true).'</dd>';
			
			print '<dt class="n" title="' . __('In experimental mode you could try new things, but they may broke', true) . '">'.__('Experimental mode', true).'</dt>';
			$value = ($user['User']['active']);
			$checked = ($user['User']['active'] == 2) ? true : false;
			print '<dd class="d">'.$form->checkbox('User.active', array('checked' => $checked, 'value' => $value)).'</dd>';

			print '</dl>';

		print '</div>';
		print $form->end(__('Submit', true));
	?>
</div>

<h3><?php __('Accounts');?></h3>
<?php
print $html->link(__('New Account', true), array('controller'=> 'accounts', 'action'=>'add'), array('class' => 'newacc'));

if(count($user['Account'])){
	foreach($user['Account'] as $account){
		print $form->create('Account', array('action' => 'edit', 'class' => 'nForm', 'id' => 'acc'.$account['id']));
		print '<div class="account_data">';
			//print $html->image('http://www.gravatar.com/avatar/'.md5(strtolower($session->read('Auth.User.email'))).'.jpg?s=60&d=' . urlencode($html->url('/img/no_avatar.png')), array('class' => 'mygravatar'));
			print '<div class="mygravatar"><span class="logo">' . $html->image('logo_'.$account['Community']['name'].'.png',
						 array(
								 'alt' => $account['Community']['name'],
								 'title' => $account['Community']['name'],
								 'class' => 'active_logo'
								 )
						 ) . '</span></div>';
			print '<dl class="fl">';
				print '<dt class="n">'.__('Community', true).'</dt>';
				print '<dd class="d">' . $html->link($account['Community']['name'], $account['Community']['web_url']) . '</dd>';
			
			print $form->input('Account.id', array('value' => $account['id']));
			
			if($account['Community']['name'] == 'facebook'){
				if(!is_array($facebookData[$account['id']]['permissions']))
					print '<p class="alert">' . $facebookData[$account['id']]['permissions'] . '</p>';
				print $form->input('username', array('value' => $account['username'],
																 'disabled' => true));
			}
			elseif($account['Community']['name'] == 'twitter'){
				print $form->input('username', array('value' => $account['username'],
																 'disabled' => true));
			}
			else{
				print $form->input('username', array('value' => $account['username']));
				print $form->input('pass_change', array('value' => false, 'type' => 'password', 'label' => __('Password', true)));
				print $form->input('pass_confirm', array('type' => 'password', 'label' => __('Password confirm', true)));
				print $form->submit(__('Edit', true));
			}
			print $html->link(__('Delete', true), array('controller' => 'accounts', 'action'=>'remove', $account['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $account['id']));
			print '</dl>';

			if(isset($errorz4v[$account['id']])){
				print '<div class="alert clb">';
				print $errorz4v[$account['id']];
				if($account['Community']['name'] == 'facebook' && $facebookData[$account['id']]['authorizeLink'])
					print $html->link(
											__('Reauthenticate', true),
											$facebookData[$account['id']]['authorizeLink'],
											array('class' => 'reauth'));
				print '</div>';
			}

		print '</div>';
		print $form->end();
	}
}
else{
	print '<p class="m">';
		__('You did not add any of your social network accounts yet. Please add at least one account what you have any of the social networks.');
	print '</p>';
}
?>